package rolesystem.jade.server;

import jade.core.*;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.DFService;

/**
 * This is an agent acting as the server for the rolesystem.
 * It uses a GUI for setting of permission matrix.
 */
public class ServerA extends Agent implements PMGui.PMOwner
{
	/**
	 * Title of GUI.
	 */
	private static final String GUI_TITLE="RoleSystem Server";

	/**
	 * Type of service, for DF.
	 */
	private static final String SERVICE_TYPE="RoleSystemServer";

	/**
	 * Maximum number of registrations that can be assigned
	 * by this server.
	 */
	private static final int MAX_REGS=10;

	/**
	 * Description of this agent by the default DF.
	 */
	private transient DFAgentDescription dfd = new DFAgentDescription();

	/**
	 * Permission matrix of the server.
	 */
	private transient PermissionMatrix permissionMatrix;

	/**
	 * GUI for setting of permission matrix.
	 */
	private transient PMGui pmGui;


	public void setup()
	{
		ServiceDescription sd = new ServiceDescription();

		// Registration with the DF
		sd.setType(SERVICE_TYPE); 
		sd.setName(getName());
		dfd.addServices(sd);
		dfd.setName(getAID());
		try
		{
			DFService.register(this, dfd);
		}
		catch(Exception fe)
		{
			System.err.println(fe);
			doDelete();
			return;
		}
		permissionMatrix=new PermissionMatrix();
		addBehaviour(new ServerBehaviour(this, permissionMatrix,
			MAX_REGS));
		pmGui=new PMGui(GUI_TITLE, permissionMatrix, this);
		System.out.println(getLocalName()+" started.");
	}


	public void takeDown()
	{
		pmGui.dispose();
		try
		{
			DFService.deregister(this, dfd);
		}
		catch(Exception fe)
		{
			System.err.println(fe);
		}
		System.out.println(getLocalName()+" is now shutting down.");
	}


	public void notifyClose()
	{
		doDelete();
	}
}
